package com.rskj.cashbook.service.iml;

import com.rskj.cashbook.dao.UserInfoDao;
import com.rskj.cashbook.iexception.BusinessException;
import com.rskj.cashbook.service.UserDetailService;
import com.rskj.cashbook.vo.UserInfo;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;

@Service
public class UserDetailsServiceIml implements UserDetailService {

    @Resource
    UserInfoDao userInfoDao;

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        UserInfo user = userInfoDao.getUserInfo(new UserInfo().setAccount(account));
        if (user == null) {
            throw new BusinessException("账号不存在");
        }
        return new User(user.getAccount(), user.getPassword(), Collections.emptyList());
    }
}
